<?php

namespace app\api\controller;

use app\admin\model\GoodsDonateFee;
use app\admin\model\Logistics1;
use app\admin\model\LogisticsFee1;
use app\common\controller\Api;
use app\admin\model\Banner;
use app\common\model\Address;
use app\common\model\Coupon;
use app\common\model\CouponLog;
use app\common\model\Goods as MyModel;
use app\common\model\Goods1;
use app\common\model\GoodsComment;
use app\common\model\GoodsDonate;
use app\common\model\GoodsSku;
use app\common\model\GoodsUsed;
use app\common\model\ShopApply;
use app\common\model\UserAddress;
use app\common\model\UserCollect;
use app\common\model\User;
use app\common\model\Activity;
use app\common\model\GoodsCategory;
use app\common\model\UserApply;
use app\common\model\UserDonate;
use app\common\model\UserReserve;
use app\common\validate\GoodsDonate as GoodsDonateValidate;
use app\common\validate\UserDonate as UserDonateValidate;
use think\cache\driver\Redis;
use think\Db;
use zop\ZopClient;
use zop\ZopProperties;
use zop\ZopRequest;
use app\common\model\GoodsDonateLeaveWord;
/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

// |----------------------------------
// | 分类
// |----------------------------------

    /*
      * 商品分类 列表
      * */
    public function GoodsCategoryList(){
        $type = input('types',0);
        $pid = input('pid',0);
        $where['status'] = 'normal';
        $where['types']  = $type;
        $where['pid']    = $pid;
        $list = GoodsCategory::where($where)->order('weigh asc')->select();
        $this->success('成功',$list);
    }


//|-----------------------------------------
//| 捐增开始
//|-----------------------------------------


    /*
     * 首页下部商品-用户捐增展示
     * 捐增商品列表
     * */

    public function GoodsDonateList(){
        $cid = $this->request->param('cid',0);
        if($cid!=0){
            $where['cid1']=$cid;
        }
        $name = $this->request->param('name');
        if(!empty($name)){
            $where['name']=['like',"%{$name}%"];
        }

        $where['status']=1;
//        $where['state']=1;

        $list = GoodsDonate::where($where)
             //   ->order('sort asc')
                ->whereNull('deletetime')
                ->field('id,deletetime,sort,name,thumbnailimage,send,cid1,is_new,createtime,state')
                ->order('createtime desc')
                ->paginate();
        foreach ($list as $k=>$v){
                $v['collect'] = UserCollect::where(['state'=>1,'status'=>1,'type'=>1,'typeid'=>$v['id']])->whereNull('deletetime')->count();
                $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
                $list[$k]=$v;
        }
        $this->success('成功',$list);
    }
    /*
     * 捐增商品详情
     * */
    /**
     * @Notes:
     * @Interface GoodsDonateInfo
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: Lw
     * @Time: 2021/1/5   9:56
     */
    public function GoodsDonateInfo(){
        $user_id = $this->auth->id;
        $goods_id = $this->request->param('goods_id');
        $where['status']=1;
//        $where['state']=1;
        $where['id']=$goods_id;
        $list = GoodsDonate::where($where)->field('id,name,shop_id,sort,thumbnailimage,thumbnailimages,cid1,goodscontent,send,province,city,area,uid,is_new,freight,price,state')->find();
        if(!$list){
            $this->error('该商品已被领取');
        }
        $list['is_collect'] =UserCollect::where(['state'=>1,'status'=>1,'type'=>1,'typeid'=>$goods_id,'uid'=>$user_id])
            ->whereNull('deletetime')
            ->count();
//        dd($list);
        $userinfo  = User::field('id,nickname,send,total_send,type,mobile')
            ->find($list['uid']);

        $list['user'] = $userinfo;
        $list['user_address'] = Address::where(['user_id'=>$user_id,'is_default'=>1])
            ->field('province,id,city,is_default as status,user_id')
            ->find();
        //浏览记录
        UserCollect::UserCollectAdd($user_id,$goods_id,1,2,1);
//        dd($user_id);
        if($user_id==$list['uid']){
            $user_id='';
        }
//        $user_id='';
        $list['GoodsDonateLeaveWord'] = GoodsDonateLeaveWord::getliu($goods_id,$user_id);
//        $list['share'] = share($goods_id,2);//分享
        $share = share($goods_id,2);//分享
        $list['qqurl'] =$share['qqurl'];//qq分享
        $list['wxurl'] =$share['wxurl'];//微信分享
//        $list['wxurl'] =$share['wxurl'];//分享
        $this->success('成功',$list);
    }
    /*
     * 发布留言
     * */
    public function GoodsDonateLeaveWordAdd(){
        $user_id = $this->auth->id;
        $goods_id = $this->request->param('goods_id');
        $content = $this->request->param('content');
        $pid = $this->request->param('pid',0);
        $Index = new Index();
        $msg = $Index->getAccessToken($content);
//        dd($msg);
        if($msg!=0){
            $this->error('请核实留言内容');
        }

        GoodsDonateLeaveWord::getAdd($goods_id,$user_id,$content,$pid);
        $this->success('成功');
    }
    /*
     * 捐增-闲置-新增
     * */
    public function GoodsDonateAdd(){
//        Logistics1::getPrice();

        $user_id = $this->auth->id;
        $data['name'] = input('name');
        $data['cid1'] = input('cid1');
        $data['price'] = input('price');
        $data['is_new'] = input('is_new');
        $data['status'] = 0;
        $data['state'] =input('state',1);;
        $data['thumbnailimages'] = input('thumbnailimages');
        $thumbnailimages = explode(',',$data['thumbnailimages']);
        $data['thumbnailimage'] = $thumbnailimages[0];
        $data['convert_price'] = $data['price'] * $data['is_new']/10;
        $data['username'] = input('username');
        $data['province'] = input('province');
        $data['city'] = input('city');
        $data['area'] = input('area');
        $data['address'] = input('address');
        $data['usemobile'] = input('usemobile');
        $data['goodscontent'] = input('goodscontent');
//        $data['weight'] = input('weight',0);//重量
        $data['uid']=$user_id;


//        $price = Logistics1::getPrice( $data['weight'], $data['province']);

//        dd($price);
//        $goodsDonateFee_id = input('goodsDonateFee_id');
//        if(!empty($goodsDonateFee_id)){
//            $GoodsDonateFee = GoodsDonateFee::get($goodsDonateFee_id);
//            $data['freight_id']=$goodsDonateFee_id;
//            $data['freight']=$GoodsDonateFee['price'];
//            $data['weight']=$GoodsDonateFee['weight'];
//        }

//        $data['weight'] = input('weight',0);//重量
        $data['freight'] = 15;//重量
        $validata = new GoodsDonateValidate;
        if (!$validata->check($data)) {
            $this->error($validata->getError());
        }
//        $data['status']=1;
        if(GoodsDonate::create($data)){
            $this->success('成功');
        }
    }

    /*
     * 捐增-闲置-修改
     * */
    public function GoodsDonateEdit(){
        $user_id = $this->auth->id;
        $data['name'] = input('name');
        $data['cid1'] = input('cid1');
        $data['price'] = input('price');
        $data['is_new'] = input('is_new');
        $data['status'] = 0;
        $data['thumbnailimages'] = input('thumbnailimages');
        $thumbnailimages = explode(',',$data['thumbnailimages']);
        $data['thumbnailimage'] = $thumbnailimages[0];
        $data['username'] = input('username');
        $data['province'] = input('province');
        $data['city'] = input('city');
        $data['area'] = input('area');
        $data['address'] = input('address');
        $data['usemobile'] = input('usemobile');
        $data['goodscontent'] = input('goodscontent');
        $data['weight'] = input('weight',0);//重量
        $data['uid']=$user_id;
        $validata = new GoodsDonateValidate;
        if (!$validata->check($data)) {
            $this->error($validata->getError());
        }

//        $goodsDonateFee_id = input('goodsDonateFee_id');
//        if(!empty($goodsDonateFee_id)){
//            $GoodsDonateFee = GoodsDonateFee::get($goodsDonateFee_id);
//            $data['freight_id']=$goodsDonateFee_id;
//            $data['freight']=$GoodsDonateFee['price'];
//            $data['weight']=$GoodsDonateFee['weight'];
//        }

        $price = Logistics1::getPrice( $data['weight'], $data['province']);
        $data['freight'] = $price;//重量

        $goods_id = $this->request->param('goods_id');
        $where['id']  =  $goods_id;
        $where['uid'] =  $user_id;

        if(GoodsDonate::where($where)->update($data)){
            $this->success('成功');
        }
    }
    /*
     * 捐增运费模版
     * */
    public function goodsDonateFee(){
        $list = GoodsDonateFee::select();
        $this->success('成功',$list);
    }
//      |-----------------------------------------
//      | 捐增结束/活动开始
//      |-----------------------------------------

    /*
     * 活动列表
     * */
    public function activitylist(){
        $where['status']=['>',0];
        $list = Activity::where($where)->whereNull('deletetime')->paginate();
        foreach ($list as $k=>$v){
            $v['starttime']=date('Y-m-d',$v['starttime']);
            $v['endtime']=date('Y-m-d',$v['endtime']);
            $v['createtime']=date('Y-m-d',$v['createtime']);
        }
        $this->success('成功',$list);
    }
    /*
     * 活动详情
     * */
    public function activityInfo(){
        $id = $this->request->param('activity_id');
        $info = Activity::get($id);

        $info['starttime']=date('Y-m-d',$info['starttime']);
        $info['endtime']=date('Y-m-d',$info['endtime']);
        $info['createtime']=date('Y-m-d',$info['createtime']);

        $where['status']=1;
        $where['state']=1;
        $info['list'] = MyModel::where('id','in',$info['goods_ids'])
            ->where($where)
            ->field('id,status,state,thumbnailimage,name,min_price')
            ->whereNull('deletetime')
            ->paginate();
        $whe['status']=1;
        $whe['state']=2;
        $whe['type']=1;
        foreach ($info['list'] as $k =>$v){
            $whe['typeid']=$v['id'];
            $v['collect']=UserCollect::where($whe)->count();
        }
//        $info['share'] = share($id,3);//分享
        $share = share($id,3);//分享
        $info['qqurl'] =$share['qqurl'];//分享
        $info['wxurl'] =$share['wxurl'];//分享
        $this->success('成功',$info);
    }


//    |------------------------------------
//    | 定向捐增-申请
//    |------------------------------------

    public function userApply(){
        $where['cate_id'] = $this->request->param('cid');
        $where['status']=2;
        $where['type']=2;
        $list = UserApply::where($where)
            ->field('id,desc,company_name')
//            ->whereNull('deletetime')
            ->paginate();
        $this->success('成功',$list);
    }
    /*
     * 定点捐增-商户-详情
     * */
    public function userApplyInfo(){
//        $where['status']=2;
//        $where['type']=2;
        $id = $this->request->request('user_apply_id');
//        $where['id'] = $id;
        $list = UserApply::getInfo($id,2);
        $list['city']=str_replace('/','',$list['city']);
        $this->success('成功',$list);
    }

    /*
     * 定点捐增 详情
     * */
    public function userApplyInfoweb(){
//        $where['status']=2;
//        $where['type']=2;
        $id = $this->request->request('user_apply_id');
////        $where['id'] = $id;
//        $list = UserDonate::getInfo($id,2);

        $where['id']=$id;
        $list = UserDonate::where($where)
            ->find();
        $rs = explode('/',$list['city']);
//        dd($rs);
        $list['province']=$rs[0];
        $list['city']=$rs[1];
        $list['area']=$rs[2];
        $this->success('成功',$list);
    }
    /*
     * 定点捐增 新增
     * */
    public function userApplyadd(){
        $data['user_id'] =$this->auth->id;
        $user_apply_id =$this->request->request('user_apply_id');
//        $cid =$this->request->request('cid');
        $data['uid'] = UserApply::where(['id'=>$user_apply_id,'status'=>2,'type'=>2])->value('user_id');
        $data['shop_id'] = $user_apply_id;
//        $data['cate_id'] = $cid;
        $data['name'] = $this->request->request('name');
        $data['images'] = $this->request->request('images');
        $data['price'] = $this->request->request('price');
        $data['is_new'] = $this->request->request('is_new');
//        $data['province'] = $this->request->request('province');
        $data['city'] = $this->request->request('city');
        $data['username'] = $this->request->request('username');
//        $data['area'] = $this->request->request('area');
        $data['address'] = $this->request->request('address');
        $data['mobile'] = $this->request->request('mobile');
        $data['status'] = 1;
        $data['order_no'] = 'DD'.order_no();
        $validata = new UserDonateValidate;
        if (!$validata->check($data)) {
            $this->error($validata->getError());
        }
        if(UserDonate::create($data)){
            $this->success('成功');
        }
    }
    /*
     * 定点捐增 修改
     * */
    public function userApplyEdit(){
        $data['user_id'] =$this->auth->id;
//        $user_apply_id =$this->request->request('user_apply_id');//商户id
        $user_donate_id =$this->request->request('user_donate_id');//捐增id
//        $cid =$this->request->request('cid');
//        $data['uid'] = UserApply::where(['id'=>$user_apply_id,'status'=>2,'type'=>2])->value('user_id');
//        dd($data);
//        $data['shop_id'] = $user_apply_id;
        $data['name'] = $this->request->request('name');
        $data['images'] = $this->request->request('images');
        $data['price'] = $this->request->request('price');
        $data['is_new'] = $this->request->request('is_new');
        $data['city'] = $this->request->request('city');
        $data['username'] = $this->request->request('username');
        $data['address'] = $this->request->request('address');
        $data['mobile'] = $this->request->request('mobile');
        $data['status'] = 1;
//        $data['order_no'] = 'DD'.order_no();
//        $validata = new UserDonateValidate;
//        if (!$validata->check($data)) {
//            $this->error($validata->getError());
//        }
        if(UserDonate::where(['id'=>$user_donate_id])->update($data)){
            $this->success('成功');
        }
        $this->error('失败');
    }

    /*
     * 定点捐增-捐赠方撤销
     * */
    public function userApplyStatus(){
        $data['user_id'] =$this->auth->id;
        $user_donate_id =$this->request->request('user_donate_id');
        $info = UserDonate::getInfo($user_donate_id);
        if(!$info){
            $this->error('捐增信息错误');
        }
        if($info['status']!=1){
            $this->error('当前捐增不可撤回');
        }
        $info->status='-3';

        if($info->save()){
            $this->success('成功');
        }
    }


//------------------------------------------------------------
// 爆品 开始
//------------------------------------------------------------

    /**
     * @DESC:
     * 获取爆品商品信息
     */
    public function getGoodsInfo(){
        $redis=new Redis();
        $user_id=$this->auth->id;
        $goods_id=input('goods_id/d');
        if (!$goods_id)$this->error('当前商品不存在');
        $goodsModel=new \app\common\model\Goods();
        $goodsInfo=$goodsModel
            ->where('id','=',$goods_id)
            ->where(['status'=>1,'state'=>1])
            ->find();
        $goodsInfo['starttime']=datetime($goodsInfo['starttime']);
        $goodsInfo['endtime']=datetime($goodsInfo['endtime']);
        $goodsInfo['earnesttime']=datetime($goodsInfo['earnesttime'],'m-d H:i:s');
        if (!$goodsInfo){
            $this->error('商品不存在');
        }
        //获取商品的优惠券
//        $couponLogModel=new CouponLog();
//        $receive=toArray($couponLogModel->where('user_id',$user_id)->column('coupon_id'));
//        $couponModel=new Coupon();
//        $coupon=$couponModel->where("FIND_IN_SET({$goods_id},goods_id)")->where('validitytime','>',time())->where(['status'=>1])->select();
//        foreach ($coupon as $key=>&$value1){
//            if (empty($receive)){
//                $value1['is_receive']=0;
//            }else{
//                if (in_array($value1['id'],$receive)){
//                    $value1['is_receive']=1;//1代表已领取优惠券
//                }else{
//                    $value1['is_receive']=0;//0代表没有领取
//                }
//            }
//        }
//        $goodsInfo['couponList']=$coupon;
        //  $goodsInfo['paramsjson']=$coupon;
        $goodsInfo['paramsjson']=json_decode($goodsInfo['paramsjson'],true);
        //查询规格
        $specList = GoodsSku::specList($goods_id,0);
        $goodsInfo['specList']=$specList;
        $sku_model = new GoodsSku();
        $skuList=toArray($sku_model->where('gid','=',$goods_id)->where('status','=',1)->whereNull('deletetime')->select());
        foreach ($skuList as $kkk1=>$vvv1){
            $vvv1['earnest']=$goodsInfo['earnest'];
            $vvv1['goods_type']=$goodsInfo['type'];
            $skuList[$kkk1]=$vvv1;
        }
//        dd($skuList);
        $redis_key='fubao_goods_id'.$user_id.'_'.$goods_id.'_0';
        //将规格信息放入缓存中
        $redis->set($redis_key,$skuList);
        //获取商品库存
        $stock=$sku_model->where('gid','=',$goods_id)->whereNull('deletetime')->sum('sku_stock');

        $goodsInfo['sku_stock']=$stock;//总库存信息
        //获取商铺信息
        $adminModel=new ShopApply();
        $shopInfo=$adminModel->where('id',$goodsInfo['shop_id'])->find();
//        if (empty($shopInfo)){
//            $shopInfo['nickname']='自营';
//            $shopInfo['avatar']='/assets/img/avatar.png';
//        }
//        $shopInfo['sell_num']=5843;

        $wheshop['typeid'] = $goodsInfo['id'];
        $wheshop['type'] = 2;
        $wheshop['status'] = 1;

        if(!empty($user_id)){  $wheshop['uid'] = $user_id; }
        $is_shop_collect= UserCollect::where($wheshop)
            ->count();
//        dd($shopInfo);
        $shopInfo1['nickname']=$shopInfo['company_name'];
        $shopInfo1['avatar']=$shopInfo['logo'];
        $shopInfo1['sell_num']=6379;
        $shopInfo1['city']=$shopInfo['city'];
        $shopInfo1['mobile']=$shopInfo['mobile'];
        $shopInfo1['is_collect']=$is_shop_collect;
        $user = User::get($shopInfo['user_id']);
        $shopInfo1['user']['total_send']=$user['total_send'];
        $goodsInfo['shopInfo']=$shopInfo1;
//        dd($shopInfo1);
        //判断当前商品是否收藏过

        $whe['typeid'] = $goodsInfo['id'];
        $whe['type'] = 1;
        $whe['status'] = 1;
        $whe['state'] = 2;
        if(!empty($user_id)){  $whe['uid'] = $user_id; }
        //收藏
//        $list['user_collect'] = UserCollect::where($whe)
//            ->count();

//        $collectModel=new \app\common\model\CollectGoods();
        $is_collect= UserCollect::where($whe)
            ->count();
        $goodsInfo['is_collect']=$is_collect;
//        if ($is_collect){
//            $goodsInfo['is_collect']=1;
//        }

        $GoodsComment =GoodsComment::where(['order_goods_id'=>$goods_id,'goods_comment.type'=>0])->with('user')->order('goods_comment.createtime desc')->find();
        if($GoodsComment){
            $GoodsComment->createtime=date('Y-m-d H:i:s',$GoodsComment->createtime);
            $GoodsComment->getRelation('user')->visible(['nickname','avatar']);
        }
        if(!empty($GoodsComment)){
            $GoodsComment = [$GoodsComment];
        }
        $goodsInfo['GoodsComment']=$GoodsComment;
        $UserReserve = UserReserve::getInfo($goods_id,$user_id);

//        $goodsInfo['GoodsComment']=[$GoodsComment];
        $goodsInfo['isReserve']=!empty($UserReserve)?1:0;
//        $goodsInfo['is_used']=0;
//        dd($goodsInfo);
//        $goodsused=new GoodsUsed();
//        $usedInfo=$goodsused->where('user_id','=',$user_id)->where('goods_id','=',$goods_id)->find();
//        if ($usedInfo){
//            $goodsInfo['is_used']=1;
//        }

        $goodsInfo['address']=Address::where(['user_id'=>$user_id,'is_default'=>1])
            ->field('province,city,user_id,is_default')
            ->find();
        //浏览记录
        UserCollect::UserCollectAdd($user_id,$goods_id,1,2,2);
        $share = share($goodsInfo['id'],0);//分享
        $goodsInfo['qqurl'] =$share['qqurl'];//分享
        $goodsInfo['wxurl'] =$share['wxurl'];//分享
        $this->success('请求成功', $goodsInfo);
    }
    /**
     * @DESC:
     * 商品切换规格
     */
    public function getGoodsAttr(){
        $goods_id=input('goods_id');
        $type=input('type',0);
        $user_id=$this->auth->id;
        $attr_symbol_path=input('sku_data');
        if(!$goods_id||!$attr_symbol_path) $this->error('请完善信息');
        $redis=new Redis();
        $redis_key='fubao_goods_id'.$user_id.'_'.$goods_id.'_'.$type;
        $list=$redis->get($redis_key);
        if (!$list){
            $this->error('请重新打开商品详情');
        }

        $exp=explode(',',$attr_symbol_path);
        sort($exp);
        $new_exp=implode(',',$exp);
        $info=[];
        foreach ($list as $key=>$value){
            if ($value['sku_data']==$new_exp){
                $info=$value;
                $info['sku_thumbnail']=get_app_image_url($info['sku_thumbnail']);
            }
        }
        $this->success('请求成功!', ['info'=>$info]);
    }
    /**
     * 爆品-商品列表
     * */
    public function goodsList(){
        $this->relationSearch = true;
        $where = [];
        $cid   = $this->request->param('cid',0);
        $name  = $this->request->param('name');
        $min_price = $this->request->param('min_price');
        $max_price = $this->request->param('max_price');
        $city1 = $this->request->param('city');
        $order = $this->request->param('order',1);
        //一级分类
        if($cid)
            $where['cid1|category_id|cid2']=$cid;

        //商品名
        if($name)
            $where['name']=['like',"%{$name}%"];

        //价格区间
        if($min_price && $max_price)
            $where['min_price']=['BETWEEN',"{$min_price},{$max_price}"];
        //发货地
        if($city1)
            $where['admin.city1']=$city1;


        switch ($order){
            case 1:
                $order = 'sort asc';
                break;
        }
//        $field = 'id,name,thumbnailimage,cid1,type,starttime,endtime,createtime,min_price';
        $list = MyModel::getList($where,$order);

        $this->success('成功',$list);
    }

    /**
     * 兑换-商品列表
     * */
    public function goods1List(){
        $this->relationSearch = true;
        $where = [];
        $cid   = $this->request->param('cid',0);
        $name  = $this->request->param('name');
        $min_price = $this->request->param('min_price');
        $max_price = $this->request->param('max_price');
        $city1 = $this->request->param('city');
        $order = $this->request->param('order',1);
        //一级分类
        if($cid)
            $where['cid1|category_id|cid2']=$cid;

        //商品名
        if($name)
            $where['name']=['like',"%{$name}%"];

        //价格区间
        if($min_price && $max_price)
            $where['min_price']=['BETWEEN',"{$min_price},{$max_price}"];
        //发货地
        if($city1)
            $where['admin.city1']=$city1;


        switch ($order){
            case 1:
                $order = 'sort asc';
                break;
        }
        $list = Goods1::getList($where,$order);

        $this->success('成功',$list);
    }
    /**
     * 商品预约
     * */
    public function userReserve(){
        $user_id = $this->auth->id;
        $goods_id = $this->request->param('goods_id');
        $sku_id = $this->request->param('sku_id');
        $type = $this->request->param('type',0);
        $buy_num = $this->request->param('buys_num');

        $sku = UserReserve::getSkuId($sku_id,$goods_id,$user_id);
        if($sku){
            $this->error('此规格已预约');
        }
        UserReserve::getAdd($goods_id,$user_id,$type,$sku_id);
        $Cache=new Redis();
        $redis = $Cache->handler();
        $redis_key= 'fubao_shop_cart_'.$user_id.'_'.$type;
//        dd($redis_key);
        $hash_key = $goods_id . '_' . $sku_id;
        if ($redis->hexists($redis_key, $hash_key)) {//判断是否之前添加过
            $redis->hincrby($redis_key, $hash_key, $buy_num);
            if ($redis->hget($redis_key, $hash_key) == 0) {
                $redis->hdel($redis_key, $hash_key);
            }
        } else {
            $redis->hset($redis_key, $hash_key, $buy_num);
        }
        $this->success('成功');
    }
    /**
     * 发货地
     * */
    public function getCity(){
        $list = ShopApply::where(['status'=>2])
            ->field('id,city1 as city')
            ->group('city1')
            ->select();
        $this->success('成功',$list);
    }
    /**
     * 爆品 结束
     * */

//----------------------------------------------------
// 兑换
//----------------------------------------------------
    /**
     * 获取兑换商品信息
     */
    public function getGoods1Info(){
        $redis=new Redis();
        $user_id=$this->auth->id;
        $goods_id=input('goods_id/d');
        if (!$goods_id)$this->error('当前商品不存在');
        $goodsModel=new \app\common\model\Goods1();
        $goodsInfo=$goodsModel
            ->where('id','=',$goods_id)
            ->where(['status'=>1,'state'=>1])
            ->find();
        if (!$goodsInfo){
            $this->error('商品不存在');
        }
        $goodsInfo['paramsjson']=json_decode($goodsInfo['paramsjson'],true);
        //查询规格
        $specList = GoodsSku::specList($goods_id,2);
        $goodsInfo['specList']=$specList;
        $sku_model = new GoodsSku();
        $skuList=toArray($sku_model->where('gid','=',$goods_id)->where('status','=',1)->whereNull('deletetime')->select());
        $redis_key='fubao_goods_id'.$user_id.'_'.$goods_id.'_2';
        //将规格信息放入缓存中
        $redis->set($redis_key,$skuList);
        //获取商品库存
        $stock=$sku_model->where('gid','=',$goods_id)->whereNull('deletetime')->sum('sku_stock');

        $goodsInfo['sku_stock']=$stock;//总库存信息
        //获取商铺信息
        $adminModel=new ShopApply();
        $shopInfo=$adminModel->where('id',$goodsInfo['shop_id'])->find();



        //判断店铺是否收藏
        $is_collect= UserCollect::is_collect($user_id,$shopInfo['id'],2,1);
        $shopInfo1['is_collect']=$is_collect;

        $shopInfo1['nickname']=$shopInfo['company_name'];
        $shopInfo1['avatar']=$shopInfo['logo'];
        $shopInfo1['sell_num']=6379;
        $shopInfo1['city']=$shopInfo['city'];
        $shopInfo1['mobile']=$shopInfo['mobile'];
        $user = User::get($shopInfo['user_id']);
        $shopInfo1['user']['total_send']=$user['total_send'];
        $goodsInfo['shopInfo']=$shopInfo1;
        //判断当前商品是否收藏过
        $whe['typeid'] = $goodsInfo['id'];
        $whe['type'] = 1;
        $whe['status'] = 1;
        $whe['state'] = 3;
        if(!empty($user_id)){  $whe['uid'] = $user_id; }
        //收藏

        $is_collect= UserCollect::is_collect($user_id,$goodsInfo['id'],1,1,3);
        $goodsInfo['is_collect']=$is_collect;

        $GoodsComment = GoodsComment::where(['order_goods_id'=>$goods_id,'goods_comment.type'=>2])->with('user')->order('goods_comment.createtime desc')->find();
        if($GoodsComment){
            $GoodsComment->createtime=date('Y-m-d H:i:s',$GoodsComment->createtime);
            $GoodsComment->getRelation('user')->visible(['nickname','avatar','total_send']);
        }
        if(!empty($GoodsComment)){
            $GoodsComment = [$GoodsComment];
        }
        $goodsInfo['GoodsComment']=$GoodsComment;

        $goodsInfo['address']=Address::where(['user_id'=>$user_id,'is_default'=>1])
            ->field('province,city,user_id,is_default')
            ->find();
        $set = get_set();
//        $v['send'] =$set['conversion'];
        $goodsInfo['send'] = $set['conversion'];
//        $goodsInfo['share'] = share($goodsInfo['id'],2);//分享
        $share = share($goodsInfo['id'],2);//分享
        $goodsInfo['qqurl'] =$share['qqurl'];//分享
        $goodsInfo['wxurl'] =$share['wxurl'];//分享
        //浏览记录
        UserCollect::UserCollectAdd($user_id,$goods_id,1,2,3);

        $this->success('请求成功', $goodsInfo);
    }


    /**
     * 商品评价列表
     * */
    public function GoodsCommentList(){
//        $user_id = $this->auth->id;
        $goods_id = $this->request->param('goods_id');
        $type = $this->request->param('type',0);
        $list = GoodsComment::getList($goods_id,$type);
        $this->success('成功',$list);
    }

//    |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//    | 店铺商品
//    |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /**
     * 店铺商品列表
     * */
    public function shopGoodsList(){
//        $this->auth->aa();

        $user_id = $this->auth->id;
        $shop_id = $this->request->param('shop_id');
        //1 综合 2 销量 3 价格升 4 价格降
//        $sort = $this->request->param('sort',1);
        //1 升 2 降
        $price_sort = $this->request->param('price_sort',0);//价格
        $sell_sort  = $this->request->param('sell_sort',0);//销量



        $cid   = $this->request->param('cid',0);
        $name  = $this->request->param('name');
        $min_price = $this->request->param('min_price');
        $max_price = $this->request->param('max_price');
        //1 爆品 2 兑换 3 捐增
        $type = $this->request->param('type',1);

        //一级分类
        if($cid)
            $where['cid1|category_id|cid2']=$cid;

        //商品名
        if($name)
            $where['name']=['like',"%{$name}%"];

        //价格区间
        if($min_price && $max_price)
            $where['min_price']=['BETWEEN',"{$min_price},{$max_price}"];

        if(!$price_sort && !$sell_sort){
            $order='sort asc';
        }else{

            if($price_sort==1){
                $price ='min_price asc';
            }
            if($price_sort==2){
                $price ='min_price desc';
            }
            if($sell_sort==1){
                $sell ='sell_num asc';
            }
            if($sell_sort==2){
                $sell ='sell_num desc';
            }
            if($price_sort && $sell_sort){
                $order =$price.','.$sell;
            }elseif ($price_sort){
                $order=$price;
            }elseif ($sell_sort){
                $order=$sell;
            }
        }

        //1 综合 2 销量升 3 销量降  4 价格升 5 价格降
        //1 综合 2 销量升 价格升 3 销量升 价格降 4 销量降 价格升 5 销量降 价格降
//        switch ($sort){
//            case 1:
//                $order='sort asc';
//                break;
//            case 2:
//                $order='sell_num asc,min_price asc';
//                break;
//            case 3:
//                $order='sell_num asc';
//                break;
//            case 4:
//                $order='min_price asc';
//                break;
//            case 5:
//                $order='min_price desc';
//                break;
//        }
        $where['shop_id'] =  $shop_id;
        $field = 'id,shop_id,sort,sell_num,min_price,cid1,category_id,cid2,name,thumbnailimage';
        switch ($type){
            case 1:
                $field = $field.',type,original_price';
                $list['goods'] = MyModel::getShopList($where,$order,$field);
                break;
            case 2:
                $list['goods'] = Goods1::getShopList($where,$order,$field);
                break;
//            case 3:
//                $list['goods'] = UserDonate::getShopList($where,$order,$field);
//                break;
        }
//        $list['goods'] = MyModel::getShopList($where,$order,$field);
        //判断当前店铺是否收藏过
        $is_collect= UserCollect::is_collect($user_id,$shop_id,2,1);
        $shop = ShopApply::where(['id'=>$shop_id])
            ->field('id,company_name as nickname,logo as avatar')
            ->find();
        $shop['is_collect'] = $is_collect;
        $shop['shop_id'] = $shop['id'];
        $shop['goodsNum'] = ShopApply::goodsNum($where);
        $share = share($shop_id,4);//分享
        $shop['qqurl'] =$share['qqurl'];//分享
        $shop['wxurl'] =$share['wxurl'];//分享
//        $list['is_collect']=$is_collect;
        $list['shop'] = $shop;
//        $list['share'] = share($shop_id,4);//分享
        $share = share($shop_id,4);//分享
        $list['qqurl'] =$share['qqurl'];//分享
        $list['wxurl'] =$share['wxurl'];//分享
        //浏览记录
        UserCollect::UserCollectAdd($user_id,$shop_id,2,2);
        $this->success('成功',$list);
    }

//-----------------------------------------------------------
// 快递
//-----------------------------------------------------------

    //快递下单
    public function express(){

//        require EXTEND_PATH."/zopsdk/ZopClient.php";
//        require EXTEND_PATH."/zopsdk/ZopProperties.php";
//        require EXTEND_PATH."/zopsdk/ZopRequest.php";
        //发件人信息
        $senderInfo['senderName']='张三';
        $senderInfo['senderProvince']='上海';
        $senderInfo['senderCity']='上海市';
        $senderInfo['senderDistrict']='青浦区';
        $senderInfo['senderAddress']='华志路';
        $senderInfo['senderMobile']='12300000001';

        //收件人信息
        $receiveInfo['receiverAddress']='永和家园';
        $receiveInfo['receiverName']='j';
        $receiveInfo['receiverDistrict']='静安区';
        $receiveInfo['receiverProvince']='上海';
        $receiveInfo['receiverCity']='上海市';
        $receiveInfo['receiverMobile']='14300000000';
        $aa =  express($senderInfo,$receiveInfo,'11111311112222');
        $data = json_decode($aa);
        if('0000'==$data->statusCode){
            $billCode = $data->result->billCode;
            dd($billCode);
        }
        dd($data);
//        $data['partnerType']='2';
//        $data['orderType']='1';
//        $data['partnerOrderCode']='122222223331231';//订单号
//        $data['senderInfo']=$senderInfo;//发件人信息
//        $data['receiveInfo']=$receiveInfo;//收件人信息
//        $data['accountInfo']['accountId']='test';//电子面单账号（测试环境传test）

//        $data['orderVasList']=[];//
//        $data['summaryInfo']=[];//
//        $data['orderItems']=[];//
//
//        $da = json_encode($data);
////        dd($da);
//        $properties = new ZopProperties(config('zhongtong.appKey'), config('zhongtong.AppSecret'));
//
////        $properties = new ZopProperties("3ba8e42ddc06ef5b8460d", "00a8f34890c00bfad409924e61b5105f");
//
//        $client = new ZopClient($properties);
//
//        $request = new ZopRequest();
//
//        $request->setUrl("https://japi-test.zto.com/zto.open.createOrder");
////
////        $request->setBody($da);
//
//        $request->setBody($da);
//
//
//        echo  $aa = $client->execute($request);
//        dd($aa);

    }
    /*
     * 快递签名生成
     * $data 请求的参数
     * */
    public function express_sign($data){
        $data = $data.config('zhongtong.AppSecret');
        return base64_encode(md5($data));
    }
    public function express_(){

    }

//--------------------------------------------------------------------
// 分享
//--------------------------------------------------------------------

//    public function share(){
//        $types = $this->request->param('type',1);//类型:0=爆品,1=捐增,2=兑换
//        $type = $this->request->param('types',1);//1 商品 2 商户
//        $id   = $this->request->param('id');//商品id/商户id
//        MyModel::getList();
//        Goods1::getList();
//    }

}
